package org.jeecg.modules.fk.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.fk.entity.BusinessDomain;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.fk.entity.DjOrg;
import org.jeecg.modules.fk.entity.IndexSet;
import org.jeecg.modules.fk.entity.dto.BusinessDomainDTO;
import org.jeecg.modules.fk.service.IBusinessDomainService;
import org.jeecg.modules.fk.service.IPointerManageService;
import org.jeecg.modules.fk.service.IIndexSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: business_domain
 * @Author: ZJJ
 * @Date: 2021-05-06
 * @Version: V1.0
 */
@Api(tags = "业务模型")
@RestController
@RequestMapping("/business_domain/businessDomain")
@Slf4j
public class BusinessDomainController extends JeecgController<BusinessDomain, IBusinessDomainService> {
    @Autowired
    private IBusinessDomainService businessDomainService;

    @Autowired
    private IIndexSetService indexSetService;

    /**
     * 分页列表查询
     *
     * @param businessDomain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "business_domain-分页列表查询")
    @ApiOperation(value = "business_domain-分页列表查询", notes = "business_domain-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BusinessDomain businessDomain,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BusinessDomain> queryWrapper = QueryGenerator.initQueryWrapper(businessDomain, req.getParameterMap());
        Page<BusinessDomain> page = new Page<BusinessDomain>(pageNo, pageSize);
        IPage<BusinessDomain> pageList = businessDomainService.page(page, queryWrapper);
        /*for (BusinessDomain record : pageList.getRecords()) {
            businessDomainService.setRelevanceIndex(record);
        }*/
        return Result.OK(pageList);
    }

    @AutoLog(value = "业务建模分页列表查询")
    @ApiOperation(value = "业务建模分页列表查询", notes = "业务建模分页列表查询")
    @GetMapping(value = "/queryList")
    public Result<?> queryList(BusinessDomainDTO dto) {
        List<BusinessDomain> list = businessDomainService.queryList(dto);
        Page<BusinessDomain> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        PageInfo<BusinessDomain> pageInfo = new PageInfo<>(list);
        page.setRecords(pageInfo.getList());
        page.setCurrent(dto.getPageNo());
        page.setTotal(pageInfo.getTotal());
        page.setSize(dto.getPageSize());
        Result<Object> result = new Result<>();
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    /**
     * 添加
     *
     * @param businessDomain
     * @return
     */
    @AutoLog(value = "business_domain-添加")
    @ApiOperation(value = "business_domain-添加", notes = "business_domain-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BusinessDomain businessDomain) {
        /*if (businessDomain.getRelevanceIndex() != null) {
            String indexs = businessDomain.getRelevanceIndex().toString();
            List<Integer> relevanceIndexs = businessDomain.getRelevanceIndexs();
            if (relevanceIndexs != null) {
                for (int i = 0; i < relevanceIndexs.size(); i++) {
                    if (relevanceIndexs.get(i) != null) {
                        indexs += "," + relevanceIndexs.get(i).toString();
                    } else if (relevanceIndexs.get(i) != null && i == relevanceIndexs.size() - 1) {
                        indexs += relevanceIndexs.get(i);
                    }
                }
            }
            businessDomain.setIds(indexs);
        }*/


        businessDomainService.save(businessDomain);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param businessDomain
     * @return
     */
    @AutoLog(value = "business_domain-编辑")
    @ApiOperation(value = "business_domain-编辑", notes = "business_domain-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BusinessDomain businessDomain) {
        businessDomainService.updateById(businessDomain);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "business_domain-通过id删除")
    @ApiOperation(value = "business_domain-通过id删除", notes = "business_domain-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        businessDomainService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "business_domain-批量删除")
    @ApiOperation(value = "business_domain-批量删除", notes = "business_domain-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.businessDomainService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "业务建模通过id查询")
    @ApiOperation(value = "业务建模通过id查询", notes = "业务建模通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        /*if (id == null || id.trim().equals("")) {
            BusinessDomain businessDomain = new BusinessDomain();
            businessDomain.setPointerManageList(pointerManageService.list());
            return Result.OK(businessDomain);
        }*/
        BusinessDomain businessDomain = businessDomainService.getById(id);
        if (businessDomain == null) {
            return Result.error("未找到对应数据");
        }
        List<IndexSet> list = indexSetService.list();
        businessDomain.setIndexSets(list);
        //businessDomain.setPointerManageList(pointerManageService.list());
        //businessDomainService.setRelevanceIndex(businessDomain);
        return Result.OK(businessDomain);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param businessDomain
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusinessDomain businessDomain) {
        return super.exportXls(request, businessDomain, BusinessDomain.class, "business_domain");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusinessDomain.class);
    }

}
